package top.lshaci.learning.thread;

import java.util.concurrent.*;

/**
 * ThreadTest
 *
 * @author lshaci
 * @since 1.0.0
 */
public class ThreadTest {

    static ExecutorService executorService = Executors.newFixedThreadPool(4);

    /**
     * 1.继承 Thread
     * 2.实现 Runnable
     * 3.实现 Callable + FutureTask 可以获取到返回值，可以处理异常
     * 4.线程池
     */
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        new Thread1().start();

        new Thread(new Thread2()).start();

        System.err.println("即将启动Callable创建的线程");
        FutureTask<String> futureTask = new FutureTask<>(new Thread3());
        new Thread(futureTask).start();
        String result = futureTask.get();
        System.err.println("main线程拿到结果了：" + result);

        executorService.execute(new Thread1());
    }

    public static class Thread1 extends Thread {
        @Override
        public void run() {
            System.err.println("当前线程ID：" + Thread.currentThread().getId());
        }
    }

    public static class Thread2 implements Runnable {
        @Override
        public void run() {
            System.err.println("当前线程ID：" + Thread.currentThread().getId());
        }
    }

    public static class Thread3 implements Callable<String> {
        @Override
        public String call() throws Exception {
            System.err.println("当前线程ID：" + Thread.currentThread().getId());
            return "我是实现的 Callable";
        }
    }
}
